<template>
  <div>
    <el-form :rules="rules" :model="loginForm" ref="loginForm" class="loginContainer">
      <h3 class="loginTitle">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text" size="normal" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" size="normal" v-model="loginForm.password" placeholder="请输入密码" @keydown.enter.native="submitLogin"></el-input>
      </el-form-item>
      <el-checkbox size="normal" class="loginRemember"></el-checkbox>
      <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>

  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123'
        },
        rules: {
          username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}]
        }
      }
    },
    methods: {
      submitLogin() {
        // 通过 refs 属性获取数据效验结果
        this.$refs.valueOf().loginForm.validate((valid) => {
          if (valid) {
            this.postKeyValueRequest('/doLogin', this.loginForm).then(resp => {
              if (resp) {
                // 将服务器响应的用户信息保存到 sessionStorage 中。（特点是：关闭窗口数据清空）
                window.sessionStorage.setItem("user",JSON.stringify(resp.obj));
                // 从路由路径中获取将要跳转的页面
                let path = this.$route.query.redirect;
                // 通过路由进行页面跳转
                this.$router.replace((path =='/' || path == undefined)?'/home':path);
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  .loginContainer {
    /*设置边框圆角*/
    border-radius: 15px;
    /*边框效果*/
    background-clip: padding-box;
    /*外边距*/
    margin: 180px auto;
    width: 350px;
    /*上 右 下 左*/
    padding: 15px 35px 15px 35px;
    background: #ffffff;
    border: 1px solid #eaeaea;
    /*添加阴影*/
    box-shadow: 0 0 25px #cac6c6;
  }

  .loginTitle {
    margin: 15px auto 20px auto;
    text-align: center;
    color: #505458;
  }

</style>